package com.yanyilu.qiubite.service;

import com.yanyilu.qiubite.entity.Gift;
import com.yanyilu.qiubite.entity.Part;
import com.yanyilu.qiubite.entity.User;
import com.yanyilu.qiubite.jpa.*;
import com.yanyilu.qiubite.util.R;
import com.yanyilu.qiubite.util.ResultCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

@Service
public class PartService {
    @Autowired
    PartJpa partJpa;

    @Autowired
    StoryJpa storyJpa;

    @Autowired
    StorylineJpa storylineJpa;

    @Autowired
    UserJpa userJpa;

    @Autowired
    GiftJpa giftJpa;

    @Autowired
    SpotJpa spotJpa;


    public List<Part> getParList(Integer storyId) {
        List<Part> list ;
        list = partJpa.findByStoryId(storyId);
        if(null == list){
            list = new ArrayList<>();
        }
        return list;
    }

    public void addPart(Part part) {
        partJpa.save(part);
    }

    @Transactional
    public void deletePart(Integer partId) {
        partJpa.deleteById(partId);

        storylineJpa.deleteByPartId(partId);
    }

    public boolean containPartIdsByUserId(Integer userid,Integer partId) {
        List<Integer> storyIds = storyJpa.findStoryIdsByUserId(userid);
        if(storyIds.size() == 0){
            return false;
        }
        List<Integer> partIds = partJpa.selectPartIdsByStoryIds(storyIds);
        return partIds.contains(partId);
    }

    public Page<Part> getListByStoryIds(String partName, Pageable pageable, List<Integer> storyIds) {
        if(storyIds.size() == 0){
            storyIds.add(0);
        }
        return partJpa.findAllByStoryIds(pageable,partName,storyIds);

    }

    public Page<Part> getList(Example<Part> example, Pageable pageable) {
        return partJpa.findAll(example,pageable);
    }

    public R getPartInfo(Integer partId) {
        Optional<Part> optional = partJpa.findById(partId);
        if(optional.isPresent()){
            return R.ok().put("part",optional.get());
        }else{
            return R.error(ResultCode.DATA_IS_WRONG);
        }
    }

    public R getParListByStroyId(Integer storyId, Pageable pageable) {
        Page<Part> pages = partJpa.findPageByStoryId(storyId,pageable);
        return R.ok().put("list",pages.getContent()).put("totalPages", pages.getTotalPages()).put("count", pages.getTotalElements());
    }

    public R endContentByPartId(Integer partId,Integer userId) {
        Optional<Part> optional = partJpa.findById(partId);
        if(optional.isPresent()){
            Part part = optional.get();
            String endContent = part.getEndContent();

            Optional<User> op = userJpa.findById(userId);
            if(op.isPresent()){
                User user = op.get();
                List<Gift> giftList = giftJpa.giftBySpotIdAndTaskTotal(user.getSpotId(),user.getTaskCount());
                if(giftList.size() > 0 && null == user.getPrizeCode()){
                    String giftword = giftList.get(0).getGiftName();
                    String giftspot = spotJpa.findById(user.getSpotId()).get().getRewardSpot();
                    endContent = endContent.replace("x奖品x",giftword);
                    endContent = endContent.replace("x地点x",giftspot);
                    return R.ok().put("endContent",endContent).put("state", 1);
                }else {
                    endContent = part.getEndContentFail();
                    endContent = endContent.replace("x数量x",user.getTaskCount().toString());
                    return R.ok().put("endContent",endContent).put("state", 0);
                }
            }else {
                return R.error(ResultCode.USER_NOT_EXIST);
            }

        }else{
            return R.error(ResultCode.DATA_IS_WRONG);
        }
    }
}
